package com.bookrecomm.dao;

import com.bookrecomm.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 评论数据访问接口
 * 提供图书评论相关的数据库操作方法
 */
@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {

    /**
     * 根据图书ID查询评论
     * @param bookId 图书ID
     * @param pageable 分页参数
     * @return 评论分页列表
     */
    @Query("SELECT c FROM Comment c WHERE c.book.id = :bookId AND c.parentId IS NULL AND c.status = 1 ORDER BY c.createdAt DESC")
    Page<Comment> findByBookId(@Param("bookId") Long bookId, Pageable pageable);

    /**
     * 根据用户ID查询评论
     * @param userId 用户ID
     * @param pageable 分页参数
     * @return 评论分页列表
     */
    @Query("SELECT c FROM Comment c WHERE c.user.id = :userId AND c.status = 1 ORDER BY c.createdAt DESC")
    Page<Comment> findByUserId(@Param("userId") Long userId, Pageable pageable);

    /**
     * 根据父评论ID查询回复
     * @param parentId 父评论ID
     * @return 回复列表
     */
    List<Comment> findByParentIdAndStatus(Long parentId, Integer status);

    /**
     * 统计图书评论数量
     * @param bookId 图书ID
     * @return 评论数量
     */
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.book.id = :bookId AND c.status = 1")
    Long countByBookId(@Param("bookId") Long bookId);

    /**
     * 统计用户评论数量
     * @param userId 用户ID
     * @return 评论数量
     */
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.user.id = :userId AND c.status = 1")
    Long countByUserId(@Param("userId") Long userId);

    /**
     * 查询热门评论（根据点赞数）
     * @param bookId 图书ID
     * @param limit 限制数量
     * @return 评论列表
     */
    @Query("SELECT c FROM Comment c WHERE c.book.id = :bookId AND c.status = 1 ORDER BY c.likes DESC")
    List<Comment> findHotComments(@Param("bookId") Long bookId, Pageable pageable);

    /**
     * 查询待审核的评论
     * @param pageable 分页参数
     * @return 评论分页列表
     */
    Page<Comment> findByStatus(Integer status, Pageable pageable);
    
    /**
     * 根据图书ID和父评论为空查询评论（一级评论）
     * @param bookId 图书ID
     * @param pageable 分页参数
     * @return 评论分页列表
     */
    @Query("SELECT c FROM Comment c WHERE c.book.id = :bookId AND c.parentId IS NULL AND c.status = 1 ORDER BY c.createdAt DESC")
    Page<Comment> findByBookIdAndParentIdIsNull(@Param("bookId") Long bookId, Pageable pageable);
    
    /**
     * 根据父评论ID列表查询回复
     * @param parentIds 父评论ID列表
     * @return 回复列表
     */
    List<Comment> findByParentIdIn(List<Long> parentIds);
    
    /**
     * 根据父评论ID删除回复
     * @param parentId 父评论ID
     */
    @Query("DELETE FROM Comment c WHERE c.parentId = :parentId")
    void deleteByParentId(@Param("parentId") Long parentId);
    
    /**
     * 查询图书的有评分的评论
     * @param bookId 图书ID
     * @return 评论列表
     */
    @Query("SELECT c FROM Comment c WHERE c.book.id = :bookId AND c.parentId IS NULL AND c.rating IS NOT NULL AND c.status = 1")
    List<Comment> findByBookIdAndParentIdIsNullAndRatingIsNotNull(@Param("bookId") Long bookId);
}
